package com.treasure.hunt.popup

import android.content.Context
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import android.webkit.WebSettings
import android.webkit.WebViewClient
import com.blankj.utilcode.util.ColorUtils
import com.treasure.hunt.R
import kotlinx.android.synthetic.main.activity_config.*
import kotlinx.android.synthetic.main.popup_dialog_hint.view.*
import kotlinx.android.synthetic.main.popup_dialog_protocol.view.*
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AlphaConfig
import razerdp.util.animation.AnimationHelper

class DialogProtocolPopup(
    context: Context,
    val onConfirm: () -> Unit,
    val onCancel: () -> Unit,
    val onProtocol: (title: String, content: String) -> Unit
) : BasePopupWindow(context) {


    init {
//        setBlurBackgroundEnable(true)
        setBackPressEnable(false)
        setOutSideDismiss(false)
    }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_dialog_protocol)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.tvDialogProtocolConfirm.setOnClickListener {
            this.onConfirm()
        }
        contentView.tvDialogProtocolCancel.setOnClickListener {
            this.onCancel()
        }

//        contentView.wvDialogProtocolWeb.webViewClient = WebViewClient()
//        contentView.wvDialogProtocolWeb.settings.apply {
//            //支持缩放，默认为true。
//            setSupportZoom(false)
//            //调整图片至适合webview的大小
//            useWideViewPort = true
//            // 缩放至屏幕的大小
//            loadWithOverviewMode = true
//            //设置默认编码
//            defaultTextEncodingName = "utf-8"
//            //设置自动加载图片
//            loadsImagesAutomatically = true
//            //允许访问文件
//            allowFileAccess = true
//            //开启javascript
//            javaScriptEnabled = true
//            //支持通过JS打开新窗口
//            javaScriptCanOpenWindowsAutomatically = true
//            //支持内容重新布局
//            layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
//            //关闭webview中缓存
//            cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
//        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.IN)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.OUT)
            .toDismiss()
    }

    fun setHintText(): DialogProtocolPopup {
        contentView.tvDialogProtocolTitle.text = "提示"
        contentView.tvDialogProtocolContent.text = "非常抱歉给您带来困扰，没有您的同意我们无法继续为您提供服务。\n确定退出？"
        contentView.tvDialogProtocolConfirm.text = "我在想想"
        contentView.tvDialogProtocolCancel.text = "退出应用"
        return this
    }

    fun setContentText(): DialogProtocolPopup {
        contentView.tvDialogProtocolTitle.text = "用户协议及隐私协议"
        contentView.tvDialogProtocolConfirm.text = "同意"
        contentView.tvDialogProtocolCancel.text = "不同意"

        val agrees: String =
            "尊敬的指尖寻宝用户：我们尊重并保护您的个人隐私，为保障您的权益，请在注册登录前完整阅读并充分理解《用户协议》和《隐私协议》以帮助您了解维护自己隐私权的方式，" +
                    "一但您开始使用本软件服务，我们将按照上述内容中约定的权利和义务来处理您的个人信息。点击“同意”即表示您已阅读并接受上述内容之约定条款，然后我们将继续为您服务。"
        val spannable = SpannableString(agrees)
        spannable.setSpan(
            object : ClickableSpan() {
                override fun onClick(widget: View) {
                    onProtocol("用户协议","user_agreement")
                }

                override fun updateDrawState(ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.color = ColorUtils.getColor(R.color.colorMain)
                    ds.isUnderlineText = false
                }
            },
            agrees.indexOf(string = "《"),
            agrees.indexOf(string = "》") + 1,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        spannable.setSpan(
            object : ClickableSpan() {
                override fun onClick(widget: View) {
                    onProtocol("隐私协议","privacy_policy")
                }

                override fun updateDrawState(ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.color = ColorUtils.getColor(R.color.colorMain)
                    ds.isUnderlineText = false
                }
            },
            agrees.lastIndexOf(string = "《"),
            agrees.lastIndexOf(string = "》") + 1,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        contentView.tvDialogProtocolContent.text = spannable
        contentView.tvDialogProtocolContent.movementMethod = LinkMovementMethod.getInstance()

        return this
    }

}